package org.club.mapper;

import org.apache.ibatis.annotations.*;
import org.club.common.model.dto.*;
import org.club.common.model.entity.Activity;
import org.club.common.model.entity.AnnouncementReadCountResult;
import org.club.common.model.entity.ClubMemberInfo;
import org.club.common.model.vo.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface ActivityMapper {
    //查询该用户管理的社团信息
    CommunityDetailsVo selectclubsTest(@Param("userId") Long userId);

    /**
     * 查询活动列表
     *
     * @param queryDTO 查询条件对象
     * @return 活动列表
     */
    List<Activity> searchActivities(ActivityQueryDTO queryDTO);

    //验证该社团是否存在
    @Select("SELECT COUNT(*) FROM clubs WHERE id = #{clubId}")
    Integer selectClubs(Long clubId);

    //创建社团活动
    Integer createActivity(ActivityRequest request);

    //查询该活动封面的旧的URL地址
    @Select("SELECT cover_url FROM activities WHERE id = #{id}")
    String selectActivityCoverUrl(Long id);

    //修改社团活动
    Integer updateActivity(ActivityRequest request);

    //查询社团活动图片URL
    @Select("SELECT cover_url FROM activities WHERE id = #{ids}")
    String selectActivityCoverUrls(Long ids);

    //删除社团活动
    Integer deleteActivity(@Param("ids") Long ids);

    //查询社团活动报名人员
    List<ApplicantsVO> getRegistration(@Param("activityId") Long activityId);

    //审核报名人员
    Integer reviewRegistrations(@Param("userIds") List<Long> userIds, @Param("status") Integer status, @Param("activityId") Long activityId);

    //查询社团成员
    List<ClubMemberInfo> getClubsUser(ClubMemberInfoDto clubMemberInfoDto);

    /**
     * 根据活动ID和用户ID列表查询报名记录
     *
     * @param activityId 活动ID
     * @param userIds    用户ID列表
     * @return 报名记录列表
     */
    List<ApplicantsVO> getRegistrationByIds(@Param("activityId") Long activityId, @Param("userIds") List<Long> userIds);

    //修改社团成员状态
    Integer removeMember(RemoveMemberDto removeMemberDto);

    //验证管理员
    @Select("SELECT COUNT(*) FROM club_admins WHERE user_id = #{adminID} AND club_id = #{clubID}")
    Integer selectAdmin(@Param("adminID") Long adminID, @Param("clubID") Long clubID);

    //验证社团成员
    @Select("SELECT COUNT(*) FROM club_members WHERE user_id = #{deleUserID} AND club_id = #{clubID}")
    Integer selectClubsUser(@Param("clubID") Long clubID, @Param("deleUserID") Long deleUserID);

    //查询社团发布的公告
    List<AnnouncementVO> getCommunityAnnouncements(ClubsAnnouncementDto clubsAnnouncementDto);

    //查询社团成员ID
    @Select("SELECT user_id FROM club_members WHERE club_id = #{clubId}")
    List<Long> getClubsUserId(@Param("clubId") Long clubId);

    //定时任务

    /**
     * 查询所有已发布但未结束的活动
     *
     * @return 活动列表
     */
    List<Activity> findPublishedActivities();

    /**
     * 更新活动状态
     *
     * @param id     活动ID
     * @param status 新状态
     * @return 影响的行数
     */
    int updateActivityStatus(@Param("id") Long id, @Param("status") Integer status);

    /**
     * 查询指定结束时间之前的已发布活动
     *
     * @param endTime 结束时间
     * @return 活动列表
     */
    List<Activity> findActivitiesEndedBefore(@Param("endTime") Date endTime);

    /**
     * 查询指定开始时间之前的未发布活动
     *
     * @param startTime 开始时间
     * @return 活动列表
     */
    List<Activity> findActivitiesToStartBefore(@Param("startTime") Date startTime);

    /**
     * 查询所有未发布的活动
     *
     * @return 活动列表
     */
    List<Activity> findUnpublishedActivities();

    /**
     * 更新社团信息
     *
     * @param literatureClub 社团信息对象
     * @return 影响的行数
     */
    int updateClubInfo(LiteratureClubDto literatureClub);

    //查询社团封面URL
    @Select("SELECT cover_url FROM clubs WHERE id = #{clubId}")
    String selectClubCoverUrl(@Param("clubId") Long clubId);

    //创建评论
    Integer createComment(ActivityCommentDTO commentDTO);

    //获取评论
    List<ActivityCommentVO> getComments(@Param("activityId") Long activityId);

    //获取社团有关消息
    List<MessageVO> getMessages(@Param("adminId") Long adminId, @Param("readStatus") Integer readStatus);

    //更新消息已读状态
    Integer updateMessageReadStatus(@Param("messageIdList") List<Long> messageIds, @Param("adminId") Long adminId);

    //获取社团消息已读数和未读数
    List<AnnouncementReadCountResult> getAnnouncementReadCountList(@Param("announcementIds") List<Long> announcementIds);

    //验证用户是否存在users表里面
    @Select("SELECT COUNT(*) FROM users WHERE id = #{userId}")
    Integer selectUser(@Param("userId") Long userId);

    //更新用户信息
    Integer updatePersonalInfo(UpdatePersonalInfoRequestDto updatePersonalInfoRequestDto);

    //查询用户信息
    @Select("SELECT * FROM users WHERE id = #{userId}")
    UserVo selectUserInfo(@Param("userId") Long userId);

    //获取评论作者ID
    @Select("SELECT user_id\n" +
            "FROM club_admins\n" +
            "WHERE club_id = (\n" +
            "    SELECT club_id\n" +
            "    FROM activities\n" +
            "    WHERE id = #{activityId}\n" +
            ");")
    Long getCommentAuthorId(@Param("activityId") Long activityId);

    //获取用户的邮箱和姓名
    List<Map<String, String>> getEmailAndName(@Param("userIds") List<Long> userIds);
    //查询出社团活动名称
    @Select("SELECT title FROM activities WHERE id = #{activityId}")
    String getActivityName(@Param("activityId") Long activityId);
//  修改社团活动报名人数
    void updateActivityNumber_applicants(@Param("activityId") Long activityId,@Param("i") Integer i);
//    查询社团活动报名人数
    Map<String, Object> selectActivityNumber_applicants(@Param("activityId") Long activityId);
    Integer selectRemainingSpots(@Param("activityId") Long activityId);
}
